package JMMDemo;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>Copyright: All Rights Reserved</p>
 * <p>Description: </p>
 *
 * @ uthor chenjinliang
 * @ date: 2019/8/21
 */
public class Demo02 {
    int a = 0;
    boolean flag = false;

    public void writer() {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        a = 1;          //1 
        System.out.println("writer: a = " + a );
        flag = true; //2 
        System.out.println("writer:  flag = " + flag);
        System.out.println("writer: a = " + a + ", flag = " + flag);
    }

    public void reader() {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (flag) {   //3 
            int i = a * a;//4
            System.out.println("reader: a = " + a + ", flag = " + flag + ", i = " + i);
        }else {
            System.out.println("reader: a = " + a + ", flag = " + flag );
        }

    }

    public static void main(String[] args) {
        Demo02 demo02 = new Demo02();
        //启动第一个线程
        new Thread(demo02 :: writer, "t1").start();
        new Thread(demo02 :: reader, "t2").start();
        java.lang.Thread.State state;
}
}
